/*
 * Copyright (c) 2011, Katholieke Universiteit Leuven
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *     * Neither the name of the Katholieke Universiteit Leuven nor the names of
 *       its contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "contiki.h"
#include "looci.h"

#ifdef LOOCI_LIGHT_SAMPLE_DEBUG
#include <stdio.h>
#ifdef CONTIKI_TARGET_AVR_RAVEN
#include <avr/pgmspace.h>
#define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
#else
#define PRINTF printf
#endif // CONTIKI_TARGET_AVR_RAVEN
#else
#define PRINTF(...)
#endif // LOOCI_LIGHT_SAMPLE_DEBUG

// printf alike things are currently broken for loadable components.
// Your mileage may vary, so you can try to uncomment this, but don't
// rely on it working.
#ifdef BUILD_COMPONENT
#undef PRINTF
#define PRINTF(...)
#endif

#define LIGHT_EVENT 105

COMPONENT_INTERFACES(light_sample, LIGHT_EVENT);
COMPONENT_NO_RECEPTACLES(light_sample);
COMPONENT(light_sample, "Light Sampling");

LOOCI_COMPONENTS(&light_sample);

COMPONENT_THREAD(light_sample, ev, data)
{
  COMPONENT_BEGIN();

  // NOTE
  // printf might not work in a loadable component
  // this depends on whether printf is used in the core or not ...
  PRINTF("Light sampling starting\r\n");

  static struct etimer et;
  static int light = 0;

  while(1) {
    etimer_set(&et, CLOCK_SECOND * 10);
    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));

    // get the (dummy) value
    light = 10;
    // NOTE
    // printf might not work in a loadable component
    // this depends on whether printf is used in the core or not ...
    PRINTF("Measured light: %i\r\n", light);
    
#ifndef LOOCI_PAYLOADBUILDER_COMPAT
    looci_event_publish(LIGHT_EVENT, (uint8_t*)&light, sizeof(light));
#else
    uint8_t buf[6];
    buf[0] = 1;
    buf[1] = 4;
    buf[2] = 0;
    buf[3] = 0;
    buf[4] = (uint8_t)((light >> 8) & 0xFF);
    buf[5] = (uint8_t)(light & 0xFF);
    looci_event_publish(LIGHT_EVENT, buf, 6);
#endif
  } 

  COMPONENT_END();
}
